package cn.stazxr.controller;

import cn.stazxr.annotation.HasPermission;
import cn.stazxr.annotation.HasRole;
import cn.stazxr.annotation.Log;
import cn.stazxr.annotation.Repeat;
import cn.stazxr.constants.DeleteFlagEnum;
import cn.stazxr.entity.YdlUser;
import cn.stazxr.exception.BusinessException;
import cn.stazxr.service.YdlUserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(YdlUser)表控制层
 *
 * @author makejava
 * @since 2025-10-28 22:51:59
 */
@Slf4j
@RestController
@RequestMapping("ydlUser")
public class YdlUserController {
	/**
	 * 服务对象
	 */
	@Autowired
	private YdlUserService ydlUserService;

	/**
	 * 分页查询
	 *
	 * @param ydlUser 筛选条件
	 * @return 查询结果
	 */
	@GetMapping
//	@Repeat(50) /* 为了演示 */
	public ResponseEntity<Page<YdlUser>> queryByPage(YdlUser ydlUser) {
		return ResponseEntity.ok(this.ydlUserService.queryByPage(ydlUser, PageRequest.of(ydlUser.getPage(), ydlUser.getSize())));
	}

	@GetMapping("/getInfo")
	public ResponseEntity<Map<String, List<String>>> getInfo() {
		try {
			return ResponseEntity.ok(this.ydlUserService.getInfo());
		} catch (BusinessException e) {
			log.info("{}", e.getMessage());
			return ResponseEntity.status(e.getCode()).build();
		}
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("{id}")
	@HasPermission("system:user:query")
	public ResponseEntity<YdlUser> queryById(@PathVariable("id") Long id) {
//		// system:user:query admin hr
//		// 获取用户，根据用户查询他的角色或者权限，去判断接口所需要的权限是否包含在用户内
//		YdlLoginUser loginUser = getLoginUser();
//		List<String> hasPerms = redisTemplate.getObject(PERM_PREFIX + loginUser.getToken(), new TypeReference<>() {});
//		if (hasPerms.contains("system:user:query")) {
//			throw new BusinessException("system:user:query: 没有权限", 403);
//		}
		return ResponseEntity.ok(this.ydlUserService.queryById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param ydlUser 实体
	 * @return 新增结果
	 */
	@PostMapping
	@HasRole({"admin", "hr"})
	@Repeat
	@Log(title = "创建用户", businessType = "用户操作")
	public ResponseEntity<YdlUser> add(@RequestBody YdlUser ydlUser, HttpServletRequest request) {
		ydlUser.setLoginIp(request.getRemoteAddr());
		ydlUser.setCreateBy(request.getHeader("userName"));
		ydlUser.setDelFlag(DeleteFlagEnum.NO.getValue());
		Date time = new Date();
		ydlUser.setCreateTime(time);
		ydlUser.setUpdateTime(time);
		return ResponseEntity.ok(this.ydlUserService.insert(ydlUser));
	}

	/**
	 * 编辑数据
	 *
	 * @param ydlUser 实体
	 * @return 编辑结果
	 */
	@PutMapping
	@Repeat
	public ResponseEntity<YdlUser> edit(@RequestBody YdlUser ydlUser) {
		return ResponseEntity.ok(this.ydlUserService.update(ydlUser));
	}

	/**
	 * 删除数据
	 *
	 * @param id 主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("{id}")
	@Repeat
	public ResponseEntity<Boolean> deleteById(@PathVariable Long id) {
		return ResponseEntity.ok(this.ydlUserService.deleteById(id));
	}

}

